package com.kanshu.ksgb.fastread.doudou.module.book.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.kanshu.ksgb.fastread.doudou.R;
import com.kanshu.ksgb.fastread.doudou.base.baseadapter.CustomFragmentAdapter;
import com.kanshu.ksgb.fastread.doudou.base.baseui.BaseFragment;
import com.kanshu.ksgb.fastread.doudou.common.net.INetCommCallback;
import com.kanshu.ksgb.fastread.doudou.common.util.Utils;
import com.kanshu.ksgb.fastread.doudou.common.view.tablayout.OnTabSelectListener;
import com.kanshu.ksgb.fastread.doudou.common.view.tablayout.SlidingTabLayout;
import com.kanshu.ksgb.fastread.doudou.module.book.bean.CategoryBean;
import com.kanshu.ksgb.fastread.doudou.module.bookcity.presenter.BookCityPresenter;
import com.umeng.analytics.MobclickAgent;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * 子分类列表页
 * Created by wcy on 2017\7\17 0017.
 */
public class SubCategoryFragment extends BaseFragment {
    public static final String EXTRA_CATEGORY_ID_1 = "category_id_1";
    public static final String EXTRA_CATEGORY_ID_2 = "category_id_2";
    public static final String EXTRA_SITE = "site";
    @BindView(R.id.tab_layout)
    SlidingTabLayout mTabLayout;
    @BindView(R.id.id_stickynavlayout_viewpager)
    ViewPager mPager;
    Unbinder mUnbinder;
    private String mCategoryId1;
    private String mCategoryId2;
    private String mSite;
    BookCityPresenter mPresenter;
    List<CategoryBean.NextCategoryBean> mNextCategoryBeans;
    List<Fragment> mFragments = new ArrayList<>();
    boolean[] mSubChanged;

    public static SubCategoryFragment newInstance(String category_id1, String category_id2, String site) {
        SubCategoryFragment fragment = new SubCategoryFragment();
        Bundle bundle = new Bundle();
        bundle.putString(EXTRA_CATEGORY_ID_1, category_id1);
        bundle.putString(EXTRA_CATEGORY_ID_2, category_id2);
        bundle.putString(EXTRA_SITE, site);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        parseArgument();
        mPresenter = new BookCityPresenter(lifeCyclerSubject);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_category_tab_list_layout, null);
        mUnbinder = ButterKnife.bind(this, view);
        return view;
    }

    private void parseArgument() {
        if (getArguments() == null) {
            return;
        }
        mCategoryId1 = getArguments().getString(EXTRA_CATEGORY_ID_1);
        mCategoryId2 = getArguments().getString(EXTRA_CATEGORY_ID_2);
        mSite = getArguments().getString(EXTRA_SITE);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        processCategoryIdRequest();
    }

    private void processCategoryIdRequest() {
        if (TextUtils.isEmpty(mCategoryId1)) {
            mTabLayout.setVisibility(View.GONE);
            mFragments.add(CategoryBooksFragment.newInstance("0", mCategoryId2, mSite));
            CustomFragmentAdapter adapter = new CustomFragmentAdapter(getChildFragmentManager(), mFragments);
            mPager.setAdapter(adapter);
            ArrayList<String> tabs = new ArrayList<>();
            tabs.add("");
            mTabLayout.setViewPager(mPager, tabs);
        } else {
            mPresenter.getSubCategories(mCategoryId1, new INetCommCallback<CategoryBean>() {
                @Override
                public void onResponse(CategoryBean categoryBean) {
                    if (categoryBean != null) {
                        ArrayList<String> tabs = new ArrayList<>();
                        if (!Utils.isEmptyList(categoryBean.next_category)) {
                            mSubChanged = new boolean[categoryBean.next_category.size()];
                            for (int i = 0; i < mSubChanged.length; i++) {
                                mSubChanged[i] = false;
                            }
                            mNextCategoryBeans = categoryBean.next_category;
                            for (CategoryBean.NextCategoryBean bean : categoryBean.next_category) {
                                tabs.add(bean.name);
                                mFragments.add(CategoryBooksFragment.newInstance(bean.parent_id, bean.category_id, bean.site));
                            }
                        } else {
                            mSubChanged = new boolean[1];
                            for (int i = 0; i < mSubChanged.length; i++) {
                                mSubChanged[i] = false;
                            }
                            tabs.add("");
                            mTabLayout.setVisibility(View.GONE);
                            mFragments.add(CategoryBooksFragment.newInstance(TextUtils.isEmpty(categoryBean.category_id) ? mCategoryId1 : categoryBean.category_id, "0", categoryBean.site));
                        }
                        CustomFragmentAdapter adapter = new CustomFragmentAdapter(getChildFragmentManager(), mFragments);
                        mPager.setAdapter(adapter);
                        mTabLayout.setViewPager(mPager, tabs);
                        mTabLayout.setOnTabSelectListener(new OnTabSelectListener() {
                            @Override
                            public void onTabSelect(int position) {
                                Fragment fragment = mFragments.get(position);
//                                if (!mSubChanged[position]) {
//                                    return;
//                                }
                                if (fragment != null && fragment.isAdded() && fragment instanceof IItemChangedListener) {
                                    ((IItemChangedListener) fragment).onClassifyIdChanged(mNextCategoryBeans.get(position).category_id);
                                    mSubChanged[position] = false;
                                }
                            }
                            @Override
                            public void onTabReselect(int position) {
                            }
                        });

                    }
                }
                @Override
                public void onError(int code, String errDesc) {
                }
            });
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    @Override
    public void onResume() {
        super.onResume();
        MobclickAgent.onResume(getActivity());
        MobclickAgent.onPageStart(getClass().getSimpleName());
    }

    @Override
    public void onPause() {
        super.onPause();
        MobclickAgent.onPause(getActivity());
        MobclickAgent.onPageEnd(getClass().getSimpleName());
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        mUnbinder.unbind();
    }
}
